﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Server.Models;
using Server.Share;
using Newtonsoft.Json;

namespace Server.Controllers
{
    [RoutePrefix("51API/Base")]
    public class BaseController : ApiController
    {
        CShare comm = new CShare();
        #region 商品管理
        /// <summary>
        /// 添加单个商品
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        [Route("CommoidtyAdd")]
        public IHttpActionResult PostCommoidtyAdd([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                CommodityModel cm = JsonConvert.DeserializeObject<CommodityModel>(dc["obj"].ToString());
                comm.bCtrl.CommoidtyAdd(cm);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base001", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 添加多个商品
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        [Route("CommoidtiesAdd")]
        public IHttpActionResult PostCommoidtiesAdd(CommodityModel cm)
        {
            try
            {
                //comm.CheckInfo(dc["Toekn"].ToString());
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base002", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="dc">obj商品模型,Token</param>
        /// <returns></returns>
        [Route("CommidtiesEdit")]
        public IHttpActionResult PostCommoidtyEdit([FromBody]Dictionary<string, Object> dc )
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                CommodityModel cm = JsonConvert.DeserializeObject<CommodityModel>(dc["obj"].ToString());
                comm.bCtrl.CommoidtyEdit(cm);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base003", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("CommidtiesDel")]
        public IHttpActionResult PostCommoidtyDel([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                comm.bCtrl.CommoidtyDel(Convert.ToInt32(dc["id"]));
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base004", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 按页查询
        /// </summary>
        /// <param name="PageNum"></param>
        /// <returns></returns>
        public IHttpActionResult PostGetCommidityByPage([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                var cm = comm.bCtrl.GetCommidityByPage(dc["PageNum"].ToString());
                RetModel ret = new RetModel
                {
                    Data = cm,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base005", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 按名称查询
        /// </summary>
        /// <param name="LikeName"></param>
        /// <returns></returns>
        [Route("GetCommidityByLikeName")]
        public IHttpActionResult PostGetCommidityByLikeName([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Toekn"].ToString());
                var cm = comm.bCtrl.GetCommidityByLikeName(dc["LikeName"].ToString(), Convert.ToInt32( dc["uid"]));
                RetModel ret = new RetModel
                {
                    Data = cm,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base006", ErrInfo = ex.Message, Data = null });
            }
        }
        #endregion
        #region 客户管理
        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        [Route("CustomerAdd")]
        public IHttpActionResult PostCustomerAdd([FromBody]Dictionary<string,Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                CustomerModel cm = JsonConvert.DeserializeObject<CustomerModel>(dc["obj"].ToString());
                comm.bCtrl.CustomerAdd(cm);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base007", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        [Route("CustomerEdit")]
        public IHttpActionResult PostCustomerEdit([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                CustomerModel cm = JsonConvert.DeserializeObject<CustomerModel>(dc["obj"].ToString());
                comm.bCtrl.CustomerEdit(cm);
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base008", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("CustomerDel")]
        public IHttpActionResult PostCustomerDel([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                comm.bCtrl.CustomerDel(Convert.ToInt32(dc["id"]));
                RetModel ret = new RetModel
                {
                    Data = null,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base009", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 按页查询
        /// </summary>
        /// <param name="PageNum"></param>
        /// <returns></returns>
        [Route("GetCustomerByPage")]
        public IHttpActionResult PostGetCustomerByPage([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                var cm = comm.bCtrl.GetCustomerByPage(dc["PageNum"].ToString());
                RetModel ret = new RetModel
                {
                    Data = cm,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base0010", ErrInfo = ex.Message, Data = null });
            }
        }
        /// <summary>
        /// 按名称查询
        /// </summary>
        /// <param name="LikeName"></param>
        /// <returns></returns>
        [Route("GetCustomerByLikeName")]
        public IHttpActionResult PostGetCustomerByLikeName([FromBody]Dictionary<string, Object> dc)
        {
            try
            {
                comm.CheckInfo(dc["Token"].ToString());
                var cm = comm.bCtrl.GetCustomerByLikeName(dc["LikeName"].ToString(), Convert.ToInt32(dc["uid"]));
                RetModel ret = new RetModel
                {
                    Data = cm,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base0011", ErrInfo = ex.Message, Data = null });
            }
        }
        #endregion
        #region 广告
        [Route("GetAdsList")]
        public IHttpActionResult  PostGetAdsList()
        {
            try
            {
                //comm.CheckInfo(dc["Token"].ToString());
                AdsManageModel[] ads = comm.bCtrl.GetAdsList();
                RetModel ret = new RetModel
                {
                    Data = ads,
                    ErrCode = "",
                    ErrInfo = "",
                    Success = true
                };
                return Ok(ret);
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Success = false, ErrCode = "Base0011", ErrInfo = ex.Message, Data = null });
            }
        }
        #endregion
    }
}